package org.BUPT.domain.model.question;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.BUPT.domain.factory.RepoFactory;
import org.BUPT.domain.service.impl.CommentServiceImpl;
import org.BUPT.infrastructure.exception.PersistException;
import org.BUPT.infrastructure.persistent.po.Comment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class CommentBO extends Commentable
{
    private int id;
    private int userId;
    private String content;
    private Date createTime;
    private Date editTime;
    private List<CommentBO> comments;


    @Getter(AccessLevel.NONE)
    CommentServiceImpl commentServiceImpl = RepoFactory.get(CommentServiceImpl.class);

//    public CommentBO(CommentBOFactory commentBOFactory) {
//        this.commentBOFactory = commentBOFactory;
//    }
    public CommentBO() throws PersistException {

    }

    public CommentBO(Comment comment) throws PersistException {
        List<Comment> comments = commentServiceImpl.getCommentsOfComment(comment.getCommentId());
        List<CommentBO> commentBOS = new ArrayList<>();
        for (Comment temp : comments) {
            CommentBO commentBO = new CommentBO(temp);
            commentBOS.add(commentBO);
        }
        this
                .setId(comment.getCommentId())
                .setContent(comment.getContent())
                .setParentId(comment.getParentId())
                .setAnswerId(comment.getAnswerId())
                .setUserId(comment.getUserId())
                .setComments(commentBOS)
                .setCreateTime(comment.getCreateTime())
                .setEditTime(comment.getUpdateTime());
    }
    private int answerId;

    private int parentId;


// -----------------------------------------------------------------

    public void save(){
        commentServiceImpl.saveComment(
            new Comment()
                .setCommentId(getId())
                .setUserId(getUserId())
                .setContent(getContent())
                .setAnswerId(getAnswerId())
                .setParentId(getParentId())
                .setCreateTime(getCreateTime())
                .setUpdateTime(getEditTime())
                .setIsDel(0)
                .setIsTop(0)
                .setIsNotify(0)
        );

    }
    public void delete(){
        // todo: 删除评论
    }
//    @JsonIgnore
//    public Commentable getParent() throws NullObjectException {
//        try{
//            if (getParentId()==-1)
//                return commentBOFactory.answerBOFactory.getAnswer(getAnswerId());
//            else
//                return commentBOFactory.getComment(getParentId());
//        }
//        catch (NullObjectException e){
//            delete();
//            throw e;
//        }
//    }



}
